﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Model;
using System.Data.Objects.SqlClient;

namespace University.Workshop.DataAccess
{
    public class DataAccessWaterPurchase
    {
        public static void CreateWaterPurchase(Model.WaterPurchase newWaterPurchase)
        {
            WFPEntities.Instance.DiscardChanges();
            WFPEntities.Instance.WaterSupply.Add(newWaterPurchase);
            WFPEntities.Instance.SaveChanges();
        }



        public static ICollection<Model.WaterPurchase> SearchWaterPurchaseByParameterAll(Model.WaterPurchase waterPurchaseToFind, String cortesia)
        {
            bool todos = false;

            if (cortesia == "Pagado")
                waterPurchaseToFind.Courtesy = false;
            else
                if (cortesia == "Cortesia")
                    waterPurchaseToFind.Courtesy = true;
                else
                    todos = true;

            if (todos)
            {
                WFPEntities.Instance.DiscardChanges();
                var q = from w in WFPEntities.Instance.WaterSupply
                        where (w.LicencePlate.Contains(waterPurchaseToFind.LicencePlate) &&
                              w.DatePurchase.Equals(waterPurchaseToFind.DatePurchase))
                        select w;
                return q.ToList();
            }
            else
            {
                WFPEntities.Instance.DiscardChanges();
                var q = from w in WFPEntities.Instance.WaterSupply
                        where (w.LicencePlate.Contains(waterPurchaseToFind.LicencePlate) &&
                                w.DatePurchase.Equals(waterPurchaseToFind.DatePurchase) &&
                              w.Courtesy.Equals(waterPurchaseToFind.Courtesy))
                        select w;
                return q.ToList();
            }
        }

        public static ICollection<Model.WaterPurchase> SearchWaterPurchaseMinusDatePurchase(Model.WaterPurchase waterPurchaseToFind, String cortesia)
        {
            bool todos = false;

            if (cortesia == "Pagado")
                waterPurchaseToFind.Courtesy = false;
            else
                if (cortesia == "Cortesia")
                    waterPurchaseToFind.Courtesy = true;
                else
                    todos = true;

            if (todos)
            {
                WFPEntities.Instance.DiscardChanges();
                var q = from w in WFPEntities.Instance.WaterSupply
                        where (w.LicencePlate.Contains(waterPurchaseToFind.LicencePlate))
                        select w;
                return q.ToList();
            }
            else
            {
                WFPEntities.Instance.DiscardChanges();
                var q = from w in WFPEntities.Instance.WaterSupply
                        where (w.LicencePlate.Contains(waterPurchaseToFind.LicencePlate) &&
                              w.Courtesy.Equals(waterPurchaseToFind.Courtesy))
                        select w;
                return q.ToList();
            }
        }


        public static ICollection<WaterPurchase> GetWaterPurchaseByLicencePlate(Model.WaterPurchase waterPurchase)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from u in WFPEntities.Instance.WaterSupply
                    where u.LicencePlate == waterPurchase.LicencePlate
                    select u).ToList();
        }

        public static void UpdateWaterPurchase(Model.WaterPurchase modifyWaterPurchase)
        {
            WFPEntities.Instance.DiscardChanges();
            Model.WaterPurchase q = (from wp in WFPEntities.Instance.WaterSupply
                            where wp.DateRegistry == modifyWaterPurchase.DateRegistry
                            select wp).First();

            q.Amount = modifyWaterPurchase.Amount;
            q.Courtesy = modifyWaterPurchase.Courtesy;
            q.DatePurchase = modifyWaterPurchase.DatePurchase;
            q.TotalPrice = modifyWaterPurchase.TotalPrice;

            WFPEntities.Instance.SaveChanges();
        }

        public static ICollection<Model.ExpenseControl> GetExpenseBetweenDates(DateTime initialDate, DateTime endDate)
        {
            WFPEntities.Instance.DiscardChanges();
            List<ExpenseControl> expenseList = new List<ExpenseControl>();
            var list = (from e in WFPEntities.Instance.WaterSupply
                        where e.DatePurchase >= initialDate && e.DatePurchase <= endDate && !e.Courtesy
                        select new Model.ExpenseControl
                        {
                            DateRegistry = e.DateRegistry,
                            SpendDate = e.DatePurchase,
                            ExpenseType = (short)(WaterExpenseType.Compra_Cisterna),
                            //ExpenseType = ((short)(e.Courtesy? WaterExpenseType.Cisterna_Cortesia : WaterExpenseType.Compra_Cisterna)),
                            StringExpenseType = "",
                            expensePaymentAmount = e.TotalPrice,
                            UserId = e.UserId,
                            Amount = e.Amount,
                            IsWaterExpense = true
                        }).ToList();

            expenseList.AddRange(list);
            return expenseList;
        }

        /*


        public static ICollection<User> GetUserByLogin(Model.User user)
        {
            WFPEntities.Instance.DiscardChanges();
            return (from u in WFPEntities.Instance.User
                    where u.Login == user.Login
                    select u).ToList();
        }

        

        public static ICollection<Model.User> SearchUserByParameter(string parameter)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = from u in WFPEntities.Instance.User
                    where u.Login.Contains(parameter) ||
                     u.Person.LastNames.Contains(parameter)

                    select u;
            return q.ToList();
        }

        public static ICollection<Model.User> GetUsersByLoginAndPassword(Model.User userToFind)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = from u in WFPEntities.Instance.User
                    where (u.Password.Equals(userToFind.Password) &&
                          u.Login.Equals(userToFind.Login) && u.IsActive)
                    select u;

            ICollection<Model.User> resultado = q.ToList();
            if (resultado.Count > 0)
                userToFind.Role = q.First().Role;

            return resultado;
        }


        public static ICollection<Model.User> SearchUserByParameterEnable(Model.User userToFind)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = from u in WFPEntities.Instance.User
                    where (u.Person.Names.Contains(userToFind.Person.Names) &&
                          u.Person.LastNames.Contains(userToFind.Person.LastNames) &&
                          u.Login.Contains(userToFind.Login)) && (u.IsActive)
                    select u;
            return q.ToList();
        }

        public static ICollection<Model.User> SearchUserByParameterDisable(Model.User userToFind)
        {
            WFPEntities.Instance.DiscardChanges();
            var q = from u in WFPEntities.Instance.User
                    where (u.Person.Names.Contains(userToFind.Person.Names) &&
                          u.Person.LastNames.Contains(userToFind.Person.LastNames) &&
                          u.Login.Contains(userToFind.Login) && (u.IsActive == false))
                    select u;
            return q.ToList();
        }

        

        

        public static void DisableUser(Model.User UserForDisable)
        {
            try
            {
                Model.User q = (from u in WFPEntities.Instance.User
                                where u.UserId == UserForDisable.UserId
                                select u).First();
                q.IsActive = false;
                WFPEntities.Instance.SaveChanges();
            }
            catch
            {
                WFPEntities.Instance.DiscardChanges();
                Model.User q = (from u in WFPEntities.Instance.User
                                where u.UserId == UserForDisable.UserId
                                select u).First();
                q.IsActive = false;
                WFPEntities.Instance.SaveChanges();
            }
        }

        public static void EnableUser(Model.User UserForEnable)
        {
            try
            {
                Model.User q = (from u in WFPEntities.Instance.User
                                where u.UserId == UserForEnable.UserId
                                select u).First();
                q.IsActive = true;
                WFPEntities.Instance.SaveChanges();
            }
            catch
            {
                WFPEntities.Instance.DiscardChanges();
                Model.User q = (from u in WFPEntities.Instance.User
                                where u.UserId == UserForEnable.UserId
                                select u).First();
                q.IsActive = true;
                WFPEntities.Instance.SaveChanges();
            }
        }*/

        public static ICollection<WaterPurchase> GetWaterPurchaseById(DateTime dateTime)
        {
            return(from e in WFPEntities.Instance.WaterSupply
                   where e.DateRegistry == dateTime
                   select e).ToList();
        }
    }
}
